/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package newsparser.site;

import java.io.Serializable;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import newsparser.ConnectorDB;
import newsparser.job.Job;
import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;

/**
 *
 * @author boyarskiy_km
 */
@ManagedBean
@SessionScoped
public class SiteAction implements Serializable{
    private List<Site> sites;
    private int siteId;
    private String siteUrl;
    private String siteCaption;
    private Site selectedSite;
    private boolean addEdButDis = false;

    public boolean isAddEdButDis() {
        return addEdButDis;
    }
    public void setAddEdButDis(boolean addEdButDis) {
        this.addEdButDis = addEdButDis;
    }
    public Site getSelectedSite() {
        return selectedSite;
    }
    public void setSelectedSite(Site selectedSite) {
        this.selectedSite = selectedSite;
    }
    public int getSiteId() {
        return siteId;
    }
    public void setSiteId(int siteId) {
        this.siteId = siteId;
    }
    public String getSiteUrl() {
        return siteUrl;
    }
    public void setSiteUrl(String siteUrl) {
        this.siteUrl = siteUrl;
    }
    public String getSiteCaption() {
        return siteCaption;
    }
    public void setSiteCaption(String siteCaption) {
        this.siteCaption = siteCaption;
    }

    
    
    
    public List<Site> getSites() {
        sites = new ArrayList<Site>();
        ConnectorDB cdb = new ConnectorDB();
        try {
            cdb.rs = cdb.stmt.executeQuery("select * from public.\"VW_SITES\"");
            while (cdb.rs.next()){
                sites.add(new Site(cdb.rs.getInt("id"), 
                                   cdb.rs.getString("url"), 
                                   cdb.rs.getString("caption")));
            }
            selectedSite = null;
            return sites;
        } catch (SQLException ex) {
            Logger.getLogger(ConnectorDB.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } finally{
            cdb.close();
        }
    }
    
    public void addSite(ActionEvent actionEvent){
        
        boolean res = false;
        String text = "{ ? = call \"addsite\"(?,?) }";
        FacesMessage msg = null; 
        ConnectorDB cdb = new ConnectorDB();
        try {
            if (siteUrl.equals("")){
                res = false;
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome", "Не заданa ссылка на сайт");
            }else{
                cdb.cstmt = cdb.conn.prepareCall(text);
                cdb.cstmt.registerOutParameter(1, Types.VARCHAR);
                cdb.cstmt.setString(2, siteUrl);
                cdb.cstmt.setString(3, siteCaption);
                cdb.cstmt.execute();
                res = true;
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome", "Site added");
            }
            FacesContext.getCurrentInstance().addMessage(null, msg);
        } catch (Exception e) {
            Logger.getLogger(ConnectorDB.class.getName()).log(Level.SEVERE, null, e);
            res = false;
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome", e.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, msg);
        } finally{
            RequestContext.getCurrentInstance().addCallbackParam("res",res);
            RequestContext.getCurrentInstance().update("formSites:panelSites");
            cdb.close();
        }
    }
 
    public void rowSelect(SelectEvent event){
        System.out.println("1");
        addEdButDis = false;
    }
}
